<template>
	<view class="content page">
		<status-bar title='我的订单'></status-bar>
		<backTop v-if='toTop'></backTop>
		<!-- 菜单 start -->
		<scroll-view v-if='delivery' class="menu" :style="{top:navHeight+'px'}" :scroll-into-view="clickNav<4?'into-0':'into-'+clickNav" scroll-x="true" scroll-with-animation='true'>
			<template v-if='delivery == "2"'>
				<view :id='"into-"+index' class="menu_itemr" v-for="(item,index) in orderTitle" :key="index" v-if='item.name2' @click="tabTitleNav(index,item.state)">
					<text :class="clickNav == index ? 'thmeColor thmeBorderBottom':'itemr'" :style="{'--color': color}">{{item.name2}}</text>
				</view>
			</template>
			<template v-else>
				<view :id='"into-"+index' class="menu_itemr" v-for="(item,index) in orderTitle" :key="index" @click="tabTitleNav(index,item.state)">
					<text :class="clickNav == index ? 'thmeColor thmeBorderBottom':'itemr'" :style="{'--color': color}">{{item.name}}</text>
				</view>
			</template>
		</scroll-view>
		<!-- 菜单 end -->
		<view class="order_list" style="padding-top: 88rpx;">
			<view class="order_item" v-for="(item,index) in orderItems" :key="index">
				<view class="order_state" v-if="item.type == 2 && item.group_state == 0 && item.state == 1">待成团</view>
				<!-- <view class="order_state" v-if="item.type == 2 && item.group_state == 1">拼团成功</view> -->
				<view class="order_state">
					<text v-if='item.state == 0'>待支付</text>
					<text v-if='item.state == 1 && item.delivery == 1 && item.group_state != 0'>待发货</text>
					<text v-if="item.state == 1  && item.delivery == 2 && item.group_state != 0" >备货中</text>
					<!-- <text v-if='item.state == 2 && item.delivery == 2'>待取货</text> -->
					<text v-if='item.state == 2 || item.state == 3'>{{item.delivery==1?'待收货':'待取货'}}</text>
					<text v-if='item.state == 4'>待评价</text>
					<text v-if='item.state == 5'>已评价</text>
					<text v-if='item.state == 6'>退款中</text>
					<text v-if='item.state == 96'>退款成功</text>
					<text v-if='item.state == 97'>已失效</text>
					<text v-if='item.state == 98'>已取消</text>
				</view>
				<view class="order_goods">
					<view class="store_name">
						<text class="iconfont icon-dianpu"></text>
						{{item.name?item.name:shopName}}
					</view>
					<template v-if='item.type == 2 && item.groupTime > 0 && item.group_state == 0'>
						<view class="order_gbuy_info">
								<view v-if='item.personalNum>0'>还差{{item.personalNum}}人成团</view>
								<view v-if='item.personalNum==0'>已满员</view>
								<view class="order_gbuy_timer">
									<count-down startTxt='剩余' endTxt='拼团' :time="item.groupTime" txtColor='#ffffff'></count-down>
								</view>
						</view>
					</template>
					<view class="store_nav flex-box align-center" v-if="item.state == 2">
						<text class="iconfont icon-daohang"></text>
						<view class="flex">
							<view class="store_nav_tel thmeColor" :style="{'--color': color}" @click="telPhone(item.utel)">{{item.utel?item.utel:service_tel}}</view>
							<view @click="openMaps(item)" class="store_nav_addr">{{item.shopAddress?item.shopAddress:shopAddress}}</view>
						</view>
						<text class="iconfont icon-xiangyouxiayiye" @click="openMaps(item)"></text>
					</view>
					<view v-if='item.goodsData' @click="toDetail(item.order_id,item.type)">
						<view style="margin-bottom: 20upx;white-space: nowrap;overflow-x: auto;">
							<view v-for='(itemImg,index_Img) in item.goodsData' :key='index_Img' class="moreImgs">	
								<image :src="imgUrl+itemImg.goods_img"></image>
								<text>x{{itemImg.goods_num}}</text>
							</view>
						</view>
					</view>
					<view class="agoods" @click="toDetail(item.order_id,item.type)" v-else>
						<view class="agoods_thumb">
							<image :src="imgUrl + item.goods_img" class="lest_img" mode="aspectFill"></image>
						</view>
						<view class="agoods_info">
							<view class="agoods_name lines2">
								<text class="speTag" v-if='item.type == 2'>团购</text>
								<text class="speTag" v-if='item.type == 1'>特价</text>
								{{item.goods_name}}
							</view>
							<view class="specs-show" v-if='item.specs_bind_text'>
								<text>规格：{{item.specs_bind_text}}</text>
							</view>
							<view class="agoods_info_info">
								<text class="myPrice">{{item.price}}</text>
								<view class="ff_a c_999999">x{{item.goods_num}}</view>
							</view>
						</view>
					</view>
					<!-- 待支付 -->
					<view class="order_oper" style="justify-content: flex-end;display: flex;">
						
						<view v-if="item.state == 0 || (item.pay_type == 2 && item.state == 1)" 
						class="order_oper_btn btn5" @click="show_model(item.order_id,index,0)">取消订单</view>
						
						<view v-if="item.state == 0"  :style="{'--color': color}"
						class="order_oper_btn btn6 thmeBg" @click="rightoffPay(item.payID)">去支付</view>
						
						<view v-if="(item.state == 1 || item.state == 2 || item.state == 3  || item.state == 5) && item.pay_type != 2" 
						class="order_oper_btn btn5" @click="show_model(item.order_id,index,1)">退货退款</view>
						<!-- pay_type:2 到付 -->
						<view v-if="(item.state == 2 || item.state == 3  || item.state == 5) && item.pay_type == 2" 
						class="order_oper_btn btn5" @click="show_model(item.order_id,index,1)">退货退款</view>
						
						
						<view v-if='(item.state == 2 || item.state == 3) && item.delivery == 1' class="order_oper_btn btn5">
							<navigator :url="'/orderPage/order/logistics?order_id='+item.order_id">查看物流</navigator>
						</view>
						
						<view v-if="(item.state == 2 || item.state == 3) && item.delivery == 2"  :style="{'--color': color}" @click="getCode(item.order_id)"
						class="order_oper_btn btn6 get_pickup_code thmeBg">提货码</view>
						
						
						<!-- <view v-if="item.state == 1  && item.delivery == 2"  
						class="order_oper_btn btn6 get_pickup_code thmeBg" :style="{'--color': color}">备货中</view> -->
						
						<view v-if="item.state == 98 || item.state == 4 || item.state == 5 || item.state == 96 || item.state == 97" class="order_oper_btn btn5"
						 @click="show_model(item.order_id,index,2)">删除订单</view>
						
						
						<!-- <view v-if="item.state == 5" class="order_oper_btn btn5"
						 @click="toUrl('/commentPage/comment/commentGoods?oid='+item.order_id)">追加评论</view> -->
						
						<view v-if="item.state == 6" class="order_oper_btn btn6 thmeBg" 
						:style="{'--color': color}" @click="refunding">退款中</view>
						
						<view v-if='(item.state == 2 || item.state == 3) && item.delivery == 1'  @click="show_model(item.order_id,index,3)"
						class="order_oper_btn btn6 thmeBg" :style="{'--color': color}">确认收货</view>
						
						<view v-if="item.state == 98 || item.state == 96" @click="buyAgain(item)"
						class="order_oper_btn btn6 thmeBg" :style="{'--color': color}">再次购买</view>
						
						<view v-if="item.state == 4" @click="toUrl('/commentPage/comment/commentGoods?oid='+item.order_id)" 
						class="order_oper_btn btn6 thmeBg" :style="{'--color': color}">立即评价</view>
						
						<!-- #ifdef MP-WEIXIN -->
						<view v-if="item.type == 2 && item.group_id != 0 && item.state == 1">
							<button :data-gid='item.goods_id' :data-group='item.group_id' :data-img='imgUrl + item.goods_img' :data-act='item.act_id' open-type="share"  
							class="order_oper_btn btn6 thmeBg" :style="{'--color': color}">去分享</button>
						</view>
						<!-- #endif -->
					</view>
				</view>
			</view>
			<view v-if='showDiLine' class="DiLine">
				------- 我也是有底线的 -------
			</view>
			<empty v-if='noData' type='1' cat='3' :msg='"还没有"+tipTXt+"订单哦 ~"'></empty>
		</view>
		
		<!-- 提货码弹窗 start -->
		<view class="mask flex-box flex-center align-center" v-if="getCodes">
			<view @click.stop class="pickup_code_pop" :style="{'background': 'url('+staticUrl+'/images/pickup_code_pop_bg.png)','background-size':'100% 100%'}">
				<view class="pickup_code_box">
					<view class="txt">请向店员出示取货二维码</view>
					<image :src="imgUrl+codeImg" class="code-img" mode="aspectFit"></image>
					<p class="num">{{pickCode}}</p>
				</view>
				<view @click="getCodes=false">
					<text class="iconfont icon-guanbi"></text>
				</view>
			</view>
		</view>
		
		<showModel type='99' v-if='showModal' 
			:title="model.title" 
			:msg='model.msg' 
			:btn1='model.btn1'
			:btn2='model.btn2'
			@certain='certain'
			@cancelFunc='showModal=false'></showModel>
	</view>
</template>

<script>
	var that;
	export default{
		data(){
			return {
				navHeight:getApp().globalData.navHeight,
				color: getApp().globalData.color,
				
				toTop:false,
				getCodes:false,
				orderTitle:[
					{name:'全部',name2:'全部',state: 666},
					{name:'待支付',name2:'待支付',state: 0},
					{name:'待发货',name2:'',state: 1},
					{name:'待收货',name2:'待核销',state: 3},
					{name:'待评价',name2:'待评价',state: 4},
					
					{name:'已评价',name2:'已评价',state: 5},
					{name:'退款/售后',name2:'退款/售后',state: 6},
					{name:'已取消',name2:'已取消',state: 98},
				],
				clickNav:0,
				orderItems:[],
				pickCode:'',
				codeImg:'',
				noData:false, // 没有数据
				page:1, // 分页页码
				more:false, // 是否启用上拉加载
				state: 666, // 当前列表的状态 666为全部订单
				vid: uni.getStorageSync('vid'),
				bid: uni.getStorageSync('bid'),
				shopName: uni.getStorageSync('startUp').title,
				service_tel:uni.getStorageSync('startUp').service_tel,
				shopAddress:uni.getStorageSync('startUp').service_address,
				
				imgUrl: this.imgUrl,
				staticUrl: this.staticUrl,
				showModal: false,
				
				
				
				showDiLine: false,
				tipTXt:'相关',
				delivery: '',
				
				modelType: -1,
				current_index:-1,
				current_order_id:'',
				model:{
					title:'',
					msg:'',
					btn1:'取消',
					btn2:'确定'
				}
			}
		},
		onBackPress() {
			if(uni.getStorageSync('path') == '1'){
				let homePage = getApp().globalData.homePage
				if(homePage.indexOf("home") > -1){
					uni.reLaunch({
					    url: '/pages/user/user'
					})
				} else {
					uni.reLaunch({
						url: homePage
					})
				}
			}
		},
		onPageScroll(e){
			if(e.scrollTop > 200){
				that.toTop = true;
			} else {
				that.toTop = false;
			}
		},
		onShow(){
			// #ifdef H5
			this.H5Share()
			// #endif
			// #ifdef MP
			uni.hideShareMenu();
			// #endif
			// 订单列表
			// this.getOrderList()
		},
		onLoad(e) {
			that = this
			if(e.status){
				that.state = e.status
				that.orderTitle.forEach((item,i)=>{
					if(item.state == that.state){
						that.clickNav = i
					}
				})
			}
			that.getOrderList();
		},
		onPullDownRefresh() {
			that.page = 1
			that.orderItems = []
			that.getOrderList()
		},
		onReachBottom() { // 加载更多
			if (that.more) {
				that.page += 1;
				that.getOrderList()
			}
		},
		methods:{
			show_model(id,index,type){
				that.current_order_id = id
				that.current_index = index
				that.modelType = type
				if(type == 0){ // 取消
					that.model={
						title:'是否确定取消订单？',
						msg:'可在已取消列表找到',
						btn1:'我再想想',
						btn2:'确定'
					}
				}
				if(type == 1){ // 退货退款
					that.model={
						title:'是否确定退货退款？',
						msg:'退货退款成功后将不可撤回',
						btn1:'我再想想',
						btn2:'退意已决'
					}
				}
				if(type == 2){ // 删除
					that.model={
						title:'是否确定删除该作品？',
						msg:'删除后将不可恢复!',
						btn1:'取消',
						btn2:'确定'
					}
				}
				if(type == 3){ // 确认收货
					that.model={
						title:'是否确定已收到宝贝？',
						msg:'确认收货后将不可撤销操作',
						btn1:'取消',
						btn2:'确认收货'
					}
				}
				that.showModal = true
			},
			certain(){ // 确定操作
				if(that.modelType == 0){ // 取消
					that.cancelSure()
				}
				if(that.modelType == 1){ // 退货退款
					that.refuseSure()
				}
				if(that.modelType == 2){ // 删除
					that.delSure()
				}
				if(that.modelType == 3){ // 确认收货
					that.receipt()
				}
			},
			buyAgain(item){ 
				/*** 再次购买***/
				if (item.goodsData) { // 多商品
				    var n = 0;
				    var len = item.goodsData.length;
				    var arr = [];
				    item.goodsData.forEach((item_) => {
						that.$http.post({
							url: '/zzj_singleSaleApi/joinCart',
							data: {
								v_id: that.vid,
								specs_bind_id: item_.specs_bind_id,
								specs_bind_text: item_.specs_bind_text,
								num: parseInt(item_.goods_num),
								goods_id: item_.goods_id,
								bid: that.bid,
							}
						}).then(res => {
							if (res.status) {
							    n++;
							    arr.push(res.car_id)
							    if (n == len) { // 循环结束                            
									var adIds = arr.join(",");
									uni.setStorageSync("buyway",'');
									uni.navigateTo({
										url:'/orderPage/order/createOrder?car_id=' + adIds
									})
							    }
							}
						})
					})
				}else{
					let param = '?goods_id=' + item.goods_id + '&goods_num=' + item.num
					if(item.specs_bind_id){
						param = param + '&specs_bind_id='+item.specs_bind_id + '&specs_bind_text=' + item.specs_bind_text
					}
					uni.navigateTo({
						url: '/orderPage/order/createOrder'+ param
					})
				}
			},
			receipt(order_id,index){ // 确认收货
				that.$http.post({
					url: '/zzj_singleSaleApi/receivingGoods',
					data: {
						order_id: that.current_order_id,
						v_id: uni.getStorageSync("vid")
					}
				}).then(res => {
					that.showModal = false
					uni.showToast({
						icon:"none",
						title: res.msg
					})
					if(res.status){
						that.$delete(that.orderItems,that.current_index);
						uni.navigateTo({
							url:'/orderPage/order/receiptSuccess?oid=' + that.current_order_id
						})
					}
				})
			},
			cancelSure(){//取消订单
				that.$http.post({
					url: '/zzj_singleSaleApi/cancelOrder',
					data: {
						v_id: uni.getStorageSync("vid"),
						order_id: that.current_order_id,
					}
				}).then(res => {
					if(res.status){
						if (that.clickNav == 0) {  // 如果当前是全部订单则修改当前订单状态，其他状态则删除该条订单
						    that.orderItems[that.current_index].state = 98;
						} else {
						    that.orderItems.splice(that.current_index, 1)
						    if (that.orderItems.length < 1) { 
								that.noData = true 
								that.showDiLine = false
							}
						}
					}
					uni.showToast({icon:"none",title: res.msg})
					that.showModal = false
				})
			},
			refuseSure(){ // 退货退款
				that.$http.post({
					url: '/zzj_singleSaleApi/refund',
					data: {
						v_id: uni.getStorageSync("vid"),
						order_id: that.current_order_id
					}
				}).then(res => {
					if (res.status) {
					    if(that.clickNav_ == 0){  // 如果当前是全部订单则修改当前订单状态，其他状态则删除该条订单
					        that.orderItems[that.current_index].state = 6;
					    }else{
					        that.orderItems.splice(that.current_index, 1)
					        if(that.orderItems.length<1){
								that.noData = true
								that.showDiLine = false
							}
					    }
					}
					uni.showToast({
						icon: 'none',
					    title: res.msg
					}) 
					that.showModal = false
				})
			},
			refunding(){
			    uni.showToast({
					icon:'none',
			        title:'退款中，请您耐心等待~'
			    }) 
			},
			delSure(){/** *删除订单* **/
				that.$http.post({
					url: '/zzj_singleSaleApi/delOrder',
					data: {
						order_id: that.current_order_id,
						v_id: uni.getStorageSync("vid")
					}
				}).then(res => {
					if(res.errcode == 100){
						that.orderItems.splice(that.current_index, 1);
						if (that.orderItems.length < 1) { 
							that.showDiLine = false
							that.noData = true 
						}
					}
					uni.showToast({icon: "none",title: res.msg})
					that.showModal = false;
				})
			},
			toDetail(order_id,type){ // 跳转详情
				uni.navigateTo({
					url:'/orderPage/order/orderDetail?order_id='+order_id
				})
			},
			tabTitleNav:function(i,state){
				//切换导航
				that.orderItems = [];
				that.page = 1; // 页码重置
				that.clickNav = i;
				if(that.delivery == '2'){
					that.tipTXt = that.orderTitle[i].name2=="全部"?'相关':that.orderTitle[i].name2
				}else{
					that.tipTXt = that.orderTitle[i].name=="全部"?'相关':that.orderTitle[i].name
				}
				that.state = state;
				that.getOrderList()
			},
			getCode:function(id){
				/** * 获取提货码* **/
				let vid = uni.getStorageSync("vid");
				that.$http.post({
					url: '/zzj_singleSaleApi/get_qrcode',
					data: {
						order_id: id,
						v_id: vid
					}
				}).then(res => {
					if(res.url){
						that.codeImg = res.url;
						that.pickCode = res.code;
						setTimeout(function(){that.getCodes = true},100)
					}
				})
			},
			telPhone:function(phoneNumber){//拨打电话
				uni.makePhoneCall({
					phoneNumber: phoneNumber
				});
			},
			openMaps:function(item){
				uni.openLocation({
					longitude: parseFloat(item.shopPointx),//要去的经度
					latitude: parseFloat(item.shopPointy),//要去的纬度
				    name: item.shopAddress,
					address: item.address
				})
			},
			getOrderList:function () { //获取订单列表
				let vid = uni.getStorageSync("vid")
				that.more = false
				that.showDiLine = false
				that.$http.post({
					url: '/zzj_singleSaleApi/orderList',
					data: {
						state: that.state,
						v_id: vid,
						page: that.page,
						pagecount: 10,
						bid: that.bid,
					}
				}).then(res => {
					that.noData = false;
					if(res.data){
						that.orderItems = that.orderItems.concat(res.data);
						if(res.data.length > 9){
							that.more = true;
						}
						if(res.data.length < 10){
							that.showDiLine = true
						}
					}else{
						if(that.orderItems.length<1){
							that.noData = true
						}
						if(that.orderItems.length > 0){
							that.showDiLine = true
						}
					}
					if(res.delivery){
						that.delivery = res.delivery
					}
					uni.stopPullDownRefresh()
				})
			},
			rightoffPay:function(payID){//去支付
			// #ifdef H5
				window.location.href ='https://sra.weikebaba.com/index.php/accountPay/weChatCashierPage?orderStr=' + payID
			// #endif
			// #ifndef H5
				uni.navigateTo({
					url: '/moneyPage/capital/checkstand?payid=' + payID
				})
			// #endif	
			}
		},
		onShareAppMessage(option){
			const vid = uni.getStorageSync("vid")
			return {
				title:'嘿 ~ 你看我买到了什么，快来跟我一起拼个团吧',
				desc:'我发现了一个宝藏商家，感觉很靠谱，你也来看看，真的很赞！',
				path:'/pages/index/index?share_v_id=' + vid + '&aid=' + option.target.dataset.act + 
				'&gid=' + option.target.dataset.gid + '&group_id=' + option.target.dataset.group +'&pageType=3',
				imageUrl: option.target.dataset.img
			}
		}
	}
</script>

<style scoped lang="scss">
	.page{
		background-color: #F8F8F8;
		.order_list {
			.order_item{
				border-radius: 10px;
				margin-bottom: 30upx;
				background: #ffffff;
				position: relative;
				.myPrice{
					font-size: 28rpx;
					color: #ff4b4e;
				}
				.order_state{
					font-size: 28upx;
					color: #ff4b4e;
					position: absolute;
					top: 30upx;
					right: 30upx;
					z-index: 1;
				}
				.order_oper{
					text-align: right;
					margin-top: 20upx;
					.btn5{
						width: 172rpx;
						height: 60rpx;
						line-height: 58rpx;
						border: 2rpx solid #dddddd;
						border-radius: 100px;
						background: none;
						appearance: none;
						-webkit-appearance: none;
						display: inline-block;
						font-size: 28rpx;
						text-align: center;
						color: #999999;
					}
					
					.btn6{
						width: 172rpx;
						height: 58rpx;
						line-height: 58rpx;
						border: none;
						border-radius: 100px;
						background: #ff4b4e;
						appearance: none;
						-webkit-appearance: none;
						display: inline-block;
						font-size: 28rpx;
						text-align: center;
						color: #ffffff;
					}
					.order_oper_btn{
						margin-left: 30upx;
					}
				}
				.order_gbuy_info{
					height: 48upx;
					border-radius: 4px;
					padding: 0 30upx;
					background: #ffbb7f;
					display: flex;
					display: -webkit-flex;
					justify-content: space-between;
					align-items: center;
					font-size: 28upx;
					font-family: arial;
					color: #ffffff;
					margin-top: 20rpx;
					margin-bottom: 10rpx;
					
				}
			}
			
		}
	}
	.agoods_info_info{
		margin-top: 10rpx;
	}
	.specs-show{
		height: 56upx;
		display: flex;
		align-items: center;
		overflow: hidden;
	}
	.specs-show text{
		font-size: 22rpx;
		color: #999;
		background-color: #EEEEEE;
		border-radius: 4upx;
		padding: 4upx 8upx;
		white-space: nowrap;
		text-overflow: ellipsis;
	}
	
	.menu_itemr{
		padding: 10rpx 30upx 0;
		display: inline-block;
		position: relative;
		text{
			font-size: 28rpx;
			display: inline-block;
			line-height: 75rpx;
		}
	}
	.itemr{
		color: #999999;
		border-bottom: 4upx solid transparent;
	}
	.moreImgs{
		display: inline-block;
		width: calc(33% - 20upx);
		height: calc(33.33vw - 42upx);
		margin-right: 20upx;
		position: relative;
	}
	.moreImgs text{
		background: rgba(0,0,0,.7);
		border-radius: 20upx;
		padding: 0 10upx;
		color: white;
		line-height: 34upx;
		position: absolute;
		right: 4upx;
		bottom: 4upx;
	}
	.moreImgs image{
		width: 100%;
		height: 100%;
		display: block;
	}
	.store_name{
		width: 590upx;
	}
	.menu{
		box-shadow: 0 0 4px 0 rgba(0,0,0,0.1);
		width: 100%;
		position: fixed;
		top: 0;
		left: 0;
		z-index: 900;
	}
	.menu::-webkit-scrollbar{
		display: none;
	}
	.pickup_code_pop{
		position: relative;
		width: 600upx;
		height: 800upx;
		padding-top: 30rpx;
		.pickup_code_box{
			width: 100%;
			height: 100%;
			.txt{
				text-align: center;
				padding: 50upx 0;
				font-size: 32upx;
			}
			.code-img{
				width: 440upx;
				height: 440upx;
				display: block;
				margin: 20rpx auto;
				background-color: #F8F8F8;
			}
			.num{
				text-align: center;
				font-size: 32rpx;
				line-height: 80rpx;
			}
		}
		.icon-guanbi{
			color: white;
			font-size: 60rpx;
			top: 20rpx;
			position: relative;
			z-index: 3;
		}
	}
	
	.agoods_thumb .lest_img{
		width: 100%;
		height: 100%;
	}
	
	
	/* 门店导航 */
	.store_nav{
		margin-bottom: 20rpx;
		margin-top: 30upx;
	}
	.store_nav .store_nav_tel{
		font-size: 28rpx;
		
		text-decoration: underline;
		color: #ff4b4e;
	}
	
	.store_nav .store_nav_addr{
		display: block;
		overflow: hidden;
		text-overflow: ellipsis;
		white-space: nowrap;
		font-size: 28rpx;
		color: #999999;
		position: relative;
		padding-top: 10rpx;
	}
	.icon-dianpu{
		margin-right: 12rpx;
		font-size: 34rpx;
	}
	.icon-daohang{
		margin-right: 12rpx;
		font-size: 42rpx;
	}
</style>
